import React, { Component } from 'react'

import { connect } from 'react-redux'

import { addAgeAction, setschoollistAction, setschoollistActionAsync } from '../../store-modules/actions/userActions'

class User extends Component {
    render() {
        const { userReducer, addAgeFn } = this.props
        return (
            <div>
                {/* user部分 */}
                <p>user页面</p>
                <p onClick={() => {
                    addAgeFn(10)
                }}>{userReducer.userinfo.name}---{userReducer.userinfo.age}</p>
                <hr />
                {/* 展示学校列表 */}
                <ul>
                    {
                        userReducer.schoollist.map(item => <li key={item.id}>{item.school_name}</li>)
                    }

                </ul>
            </div>
        )
    };
    componentDidMount() {
        // 调用请求学校列表
        this.props.setschoollistFn()
    }
}

// 定义map函数
function mapStateToProps(state) {
    return state
}

function mapDispatchToProps(dispatch) {
    return {
        // 定义修改store中state数据的方法
        addAgeFn(payload) {
            dispatch(addAgeAction(payload))
        },
        // 修改学校列表
        setschoollistFn() {
            dispatch(setschoollistActionAsync())
        }

    }
}
export default connect(mapStateToProps, mapDispatchToProps)(User)